<?php
Session_start();
$sessionId = session_id();//得到sessionid
//添加團購頁面
include("../common.inc.php");
InitGP(array("action","xml")); //初始化變量全局返回
include("function_common.php");

$titlestr="添加團購信息";
//初始化團購對象
include(INC_PATH."/group.class.php");
$gp=new GroupClass();
require_once (INC_PATH.'/attach.class.php');
$att=new AttachClass();
include_once(INC_PATH."/suppliers.class.php");
$sp=new SupplierClass();
if(empty($action)){
	if(GetNum($xml)){
	//獲取xml解析
		$tuan518="http://www.tuan518.com/";//設置服務網站需要修改
		
		$xmlurl=$tuan518."api_xml.php?gid=".$xml;
		$sitename=urlencode($cfg_site_name);
		$recordurl=$tuan518."api_record.php?gid=".$xml."&sitename={$sitename}&siteurl=".SITE_URL;
		
		include_once(INC_PATH.'/fun_xml.php');
		include_once(INC_PATH.'/httpdown.class.php');
		$htd=new HttpDown();
		$htd->OpenUrl($recordurl);
		$htd->GetHtml();
		
		$htd->OpenUrl($xmlurl);
		$data=$htd->GetHtml();
		$data=XML_unserialize($data);
		$show=false;
		if(!empty($data)){
			foreach ($data['data'] as $key=>$value){
				$data['data'][$key]=iconv("utf-8",CHARSET,$value);
				if ($key=="imgarray") {
					$data['data'][$key]=unserialize($value);
				}
			}
			$arrayimg=array();
			foreach($data['data']['imgarray'] as $key=>$value){
			 $tempimg=GetRemoteImage($tuan518.$value['filepath']);
			 $thumbname="_thumb_".$tempimg['filename'];
			 $tempthumb=GetRemoteImage($tuan518.$value['thumb'],$thumbname);
			 if(!empty($tempimg) && !empty($tempthumb)){
				$aid=$att->add($sessionId,$tempimg['filename'],$tempimg['filepath'],$value['filesize'],$value['fileext'],$tempthumb['filepath']);//存入數據庫
				$arrayimg[$key]['filepath']=$tempimg['filepath'];
				$arrayimg[$key]['thumb']=$tempthumb['filepath'];
				$arrayimg[$key]['aid']=$aid;
			 }
			}
			//數據和圖片
			$xmldata=$data['data'];
			$show=true;
			//添加供貨商家信息
			$arrayinsert=array(
				"name"=> Char_cv($xmldata['name']),
				"web"=> $xmldata['web'],
				"address"=> Char_cv($xmldata['address']),
				"map"=>Char_cv($xmldata['map']) ,
				"tel"=> Char_cv($xmldata['tel']),
				"operating_time"=> Char_cv($xmldata['operating_time']),
				"codebar"=> Char_cv($xmldata['codebar']),
				"resolution"=> GetNum($xmldata['resolution']),
				"bus"=> Char_cv($xmldata['bus']),
				"brief"=> Char_cv($xmldata['brief']),
				"about"=>HtmlReplace($xmldata['about'],-1),
				"pwd"=> Char_cv($xmldata['pwd'])
			);
			$sid=$sp->add($arrayinsert);	
		}
		
		//print_r($data['data']);
		//print_r($arrayimg);
		//exit;
	}
	
	$sidselect=$sp->getselect("sid",$sid);//獲取select
	include_once(INC_PATH."/city.class.php");
	$city=new CityClass();
	$cityselect=$city->getselect("cid");
	
	//包含後台模板文件
	include("tpl/groups_add.html");
}elseif ($action=="save"){
	InitGP(array("sid","cid","title","subtitle","local","sn","cityid","gtypeid","freefreight","order","weight","score","market_price","shop_price","earnest_money","stock","max_buynum","group_usernum","group_startnum","group_bond_end_time","group_begin_time","group_end_time","gallery_id","goods_gallerys","goods_reviews","goods_reviews_contents","goods_reviews_site","goods_brief","goods_about","seokeyword","seocontent","goods_reviews_content","goods_reviews_user","goods_reviews_url","goods_reviews_web")); //初始化變量全局返回
	if(isset($sid) && isset($title)){
		//print_r($goods_reviews_web);
		//print_r($goods_reviews_user);
		//print_r($goods_reviews_url);
		//print_r($goods_reviews_content);
		//print_r($gallery_id);

		
		
		
		$maxnum=max(max(count($goods_reviews_url),count($goods_reviews_content)),max(count($goods_reviews_web),count($goods_reviews_user)));
		//echo $maxnum;
	
		$contents=array();
		for ($i=0;$i<$maxnum;$i++){
			$contents[$i]['web']=$goods_reviews_web[$i];
			$contents[$i]['user']=$goods_reviews_user[$i];
			$contents[$i]['url']=$goods_reviews_url[$i];
			$contents[$i]['content']=$goods_reviews_content[$i];
		}
		//print_r($goods_reviews_contents);
		$goods_reviews_contents=serialize($contents);
		//print_r($goods_reviews_contents);
		//print_r(unserialize($goods_reviews_contents));
		
		if(is_array($goods_gallerys))$good_pics=getdotstring($goods_gallerys,'int');//格式化成id,id形式
		
		//處理數據
		$arrayinsert=array(
			"sid"=> GetNum($sid),
			"cityid"=> GetNum($cid),
			"sn"=> Char_cv($sn),
			"title"=> Char_cv($title) ,
			"subtitle"=> Char_cv($subtitle) ,
			"local"=> $local ,
			"gtypeid"=> GetNum($gtypeid),
			"freefreight"=>GetNum($freefreight) ,
			"order"=> GetNum($order),
			"weight"=> GetNum($weight),
			"shop_price"=> GetNum($shop_price),
			"market_price"=> GetNum($market_price),
			"earnest_money"=> GetNum($earnest_money),
			"score"=> GetNum($score),
			"stock"=> GetNum($stock),
			"max_buynum"=> GetNum($max_buynum),
			"goods_pic"=> GetNum($gallery_id),
			"goods_pics"=> $good_pics,
			"group_usernum"=> GetNum($group_usernum),
			"group_startnum"=> GetNum($group_startnum),
			"group_bond_end_time"=> strtotime($group_bond_end_time),
			"group_begin_time"=> strtotime($group_begin_time),
			"group_end_time"=>  strtotime($group_end_time),
			"goods_reviews"=>  HtmlReplace($goods_reviews,-1),
			"goods_reviews_contents"=>  HtmlReplace($goods_reviews_contents,-1),
			"goods_reviews_site"=>  HtmlReplace($goods_reviews_site,-1),
			"goods_brief"=>  HtmlReplace($goods_brief,-1),
			"goods_about"=>  HtmlReplace($goods_about,-1),
			"seokeyword"=>  HtmlReplace($seokeyword,-1),
			"seocontent"=>  HtmlReplace($seocontent,-1)
		);
		$info=$gp->add($arrayinsert);
		if($info=="OK"){
			showmsg("添加團購成功!","groups_list.php");//成功
		}else showmsg($info,"-1");//出錯！
	}else showmsg("缺少參數","-1");//出錯！
}elseif($action=="ajaxuploadimg"){
	AjaxHead();
	//ajax上傳圖片處理
	require_once (INC_PATH.'/upload.class.php');
	$f = new Upload();
	$f->run('Filedata',1);
	$info=$f->getInfo();
	//print_r($info);
	$i=$info[0];

	$aid=$att->add($sessionId,$i['saveName'],$i['fullsavename'],$i['size'],$i['ext'],$i['fullthumbname']);//存入數據庫
	if($aid){
	$i['fullsavename']="../".$i['fullsavename'];
	$i['fullthumbname']="../".$i['fullthumbname'];
	/*
	echo "POST";
	print_r($_POST);
	echo "GET";
	print_r($_GET);
	echo "FILES";
	*/
	$data['data']=array("big_img"=>$i['fullsavename'],"id"=>$aid,"small_img"=>$i['fullthumbname']);
	$data['msg']="";
	}else $data['msg']="error";
	echo json_encode($data);//編碼輸出
	
}elseif($action=="ajaxgetimg"){
	AjaxHead();
	//獲取ajax上傳的圖片列表


}elseif ($action=="ajaxdelimg"){
	AjaxHead();
	InitGP(array("aid")); //初始化變量全局返回
	if(is_numeric($aid)){
		require_once (INC_PATH.'/attach.class.php');
		$att=new AttachClass();
		$att->del($aid);
		echo "OK";
	}
}elseif($action=="ajaxgetbigimg"){
	AjaxHead();
	InitGP(array("aid"));//初始化aid

	require_once (INC_PATH.'/attach.class.php');
	$att=new AttachClass();
	$row=$att->getdata("","aid={$aid}");
	print('../'.$row[0]['filepath']);
}


?>